home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_200
/
247_02
/
ratcalc.c
< prev
next >
Wrap
Text File
|
1989-04-17
|
21KB
|
786 lines
/*
* MIRACL RATIONAL CALCULATOR - IBM-PC VERSION
* Compiles with Turbo C V1.0 or Microsoft C V3.0+
* ** Change display mode for 40/80 column operation (e.g. 'mode 40' to DOS)
* ** Change colour/B&W below
*/
#include <stdio.h> /*** IBM-PC specific section ***/
#include <dos.h>
#include "miracl.h"
#define ESC 27
#define SPACE 32
#define BELL 7
#define DVERT 186
#define DHORZ 205
#define DTLHC 201
#define DTRHC 187
#define DBLHC 200
#define DBRHC 188
#define VERT 179
#define HORZ 196
#define TLHC 218
#define TRHC 191
#define BLHC 192
#define BRHC 217
#define LSIDE 199
#define RSIDE 182
/* Globals */
/* set colours B/W Colours (suggested) */
int ORDINARY=0x07; /* 0x07 0x17 blue-white */
int INVER =0x70; /* 0x70 0x40 red-black */
int BOLD =0x0F; /* 0x0F 0x70 white-black */
int BLINKING=0x87; /* 0x87 0xF4 white-red (blink) */
int HELPCOL =0x07; /* 0x07 0x02 black-green */
int PRESSED =0x0F; /* 0x0F 0x4F red-white (bold) */
int STATCOL =0x07; /* 0x07 0x74 white-red */
int BGROUND =0x07; /* 0x07 0x07 black-white */
int dmode;
/*** Constants 16-bit values ***/
char cpi[] = "2646693125139304345/842468587426513207";
char clg2[]= "28575556407596679551/41225813519882571900";
char clg10[]="9948944967727459672/4320771900243161573";
/*** Device independent data ***/
extern big w15;
char *settings[4][4]= {" ","HYP","","",
"Be ","B2 ","B10","",
"RAD","DEG","GRA","",
"DEC","HEX","OCT","BIN"};
int nops[]= {1,2,1,3}; /* number of options */
int opp[] = {0,1,1,2,2,3,3,2}; /* operator precedence */
char *keys[6][7]= {"SIN","COS","TAN","EXP","F-D","CLR","OFF",
"ASN","ACS","ATN","LOG","SET","MOD","DEL",
" 7 "," 8 "," 9 ","Y^X","X√Y"," ÷ "," RM", /******/
" 4 "," 5 "," 6 "," X²","²√X"," x "," M+",
" 1 "," 2 "," 3 ","1/X"," π "," - ","+/-",
" 0 "," / "," . "," ( "," ) "," + "," = "};
int qkeys[6][7]= {'s','k','t','e','f','c','o',
'S','K','T','l','g','%','<',
'7','8','9','y','x','\\','r',
'4','5','6','q','v','*','m',
'1','2','3','i','p','-',';',
'0','/','.','(',')','+','='};
char *htext[]= {"Arrow keys find, space bar activates OR",
"use numeric keys (with A-F for hex),",
"brackets, and mnemonic keys as below: ",
" FUNCTION KEY FUNCTION KEY",
" SIN s ASN S ",
" COS k ACS K ",
" TAN t ATN T ",
" EXP(e/2/10) e LOG(e/2/10) l ",
" Y^X y X√Y x ", /******/
" X² q ²√X v ",
" 1/X i π p ",
" F-D f SET g ",
" MOD % CLR c ",
" ÷ d or \\ x b or * ",
" - - + , or + ",
" OFF o DEL DEL or < ",
" RM r M+ m ",
" +/- ; = RET or = ",
"Note: / means 'over' as in fractions ",
"F-D converts fraction <--> decimal ",
"SET changes settings - use arrow keys ",
"Alternative arrow keys - u h j n ",
"Report any problems to M.Scott |NIHED ",
"This program is Public Domain |Dublin "}; /******/
char display[]= " MIRACL RATIONAL CALCULATOR V2.0 ";
char status[]= " ";
char oldstat[]= " ";
int erptr=0;
int mmptr=6;
int exptr=10;
int typtr=16;
int stptr[]={22,26,30,34};
int dbeg=2;
int dlen=37;
int top=7;
int width=80; /* screen width can be 40 or 80 columns */
char mybuff[40];
flash x,y[8],m,t;
flash radeg,loge2,loge10;
int ipt,op[8],prop[8],sp,brkt,lgbase;
bool flag,newx,result,hyp,degrees,delim;
int option[]={0,0,0,0};
/* Device specific code - IBM-PC versions */
void curser(x,y)
int x,y;
{ /* position cursor at x,y */
union REGS regs;
regs.h.ah=2;
regs.h.dh=y-1;
regs.h.dl=x-1;
regs.h.bh=0; /* Video page 0 */
int86(0x10,®s,®s); /* Use Dos Interrupt 10h */
}
void screen()
{ /* initialise screen */
union REGS regs;
regs.h.ah=0x0F; /* get screen mode */
regs.h.al=0;
int86(0x10,®s,®s);
dmode=regs.h.al;
if ((dmode&2)!=0) width=80;
else width=40;
regs.h.ah=6; /* clear screen */
regs.h.al=0;
regs.h.cl=0;
regs.h.ch=0;
regs.h.dl=width-1;
regs.h.dh=24;
regs.h.bh=BGROUND;
regs.h.bl=0;
int86(0x10,®s,®s);
}
void restore()
{ /* restore situation */
union REGS inregs,outregs;
inregs.h.ah=0; /* reset initial display mode */
inregs.h.al=dmode;
int86(0x10,&inregs,&outregs);
}
void apchar(attr,x,y,ch)
int attr,x,y;
char ch;
{ /* output character with attribute at x,y */
union REGS regs;
curser(x,y);
regs.h.ah=9; /* output a character */
regs.h.al=ch;
regs.h.bh=0;
regs.h.bl=attr;
regs.h.cl=1;
regs.h.ch=0;
int86(0x10,®s,®s);
}
void aprint(attr,x,y,text)
int attr,x,y;
char* text;
{ /* attribute print */
while (*text!='\0')
{
apchar(attr,x++,y,*text);
text++;
}
}
void lclr(x,y)
int x,y;
{ /* clear from x,y to end of line */
aprint(BGROUND,x,y," ");
}
void cset(k)
int k;
{ /* select special character set */
return;
}
int gethit()
{ /* get single keystroke */
int ch;
ch=getch();
if (ch!=0) return ch;
ch=getch();
if (ch==72) return 'u'; /* transform some useful extended codes */
if (ch==75) return 'h';
if (ch==77) return 'j';
if (ch==80) return 'n';
if (ch==83) return 127;
return 0;
}
/*** Device independent code ***/
int arrow(c)
int c;
{ /* check for arrow key *
* returns 1 for up, 2 for down, *
* 3 for right, 4 for left, else 0 */
if (c=='u') return 1;
if (c=='n') return 2;
if (c=='j') return 3;
if (c=='h') return 4;
return 0;
}
void instat(ptr,strg)
int ptr;
char *strg;
{ /* insert a status setting into status line */
strncpy(&status[ptr],strg,strlen(strg));
}
void getstat()
{ /* set status line */
int i;
if (ERNUM) instat(erptr,"ERROR");
else instat(erptr," ");
if (EXACT) instat(exptr,"EXACT");
else instat(exptr," ");
if (size(m)!=0) instat(mmptr,"MEM");
else instat(mmptr," ");
if (POINT) instat(typtr,"POINT");
else instat(typtr,"FRACT");
for (i=0;i<4;i++)
instat(stptr[i],settings[i][option[i]]);
}
void setopts()
{ /* set options */
if (option[0]==0) hyp=FALSE;
else hyp=TRUE;
lgbase=0;
if (option[1]==1) lgbase=2;
if (option[1]==2) lgbase=10;
if (option[2]==0) degrees=FALSE;
else degrees=TRUE;
IOBASE=10;
if (option[3]==1) IOBASE=16;
if (option[3]==2) IOBASE=8;
if (option[3]==3) IOBASE=2;
}
void show(force)
bool force;
{ /* output display */
if (force || strcmp(oldstat,status)!=0)
{
if (ERNUM) aprint(BLINKING,2,2,status);
else aprint(STATCOL,2,2,status);
strcpy(oldstat,status);
}
aprint(BOLD,dbeg,3,display);
}